
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import fire from '@dfeidao/atom-web/msg/fire';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import storage_get from '@dfeidao/atom-web/storage/get';
import update_pwd from '../../atom/update-pwd';

export default async function a004(fd: IFeidaoAiBrowserComponent) {
	// 修改密码;
	const user = storage_get('user');
	const current_pwd = fd.data.node.querySelector('[data-feidao-id="current_pwd"]') as HTMLInputElement;
	if (!current_pwd.value) {
		error('请输入当前密码');
		return;
	}
	const reg = /^[a-zA-Z0-9]{6,18}$/;
	const new_pwd = fd.data.node.querySelector('[data-feidao-id="new_pwd"]') as HTMLInputElement;
	if (!new_pwd.value) {
		error('请输入新密码');
		return;
	}
	if (!reg.test(new_pwd.value)) {
		error('密码格式为6-16位数字或字母');
		return;
	}
	const sure_pwd = fd.data.node.querySelector('[data-feidao-id="sure_pwd"]') as HTMLInputElement;
	if (!sure_pwd.value) {
		error('请输入确认密码');
		return;
	}

	if (new_pwd.value !== sure_pwd.value) {
		error('确认密码与新密码不一致');
		return;
	}
	if (new_pwd.value === current_pwd.value) {
		error('新密码与旧密码一致');
		return;
	}
	const productid = user.productid;
	const edit_type = 'normal';
	const id = user.usercode;
	const captcha = current_pwd.value;// 旧密码
	const password = new_pwd.value;// 新密码
	const res = await update_pwd(productid, edit_type, id, password, captcha) as { msg: string, result: boolean };
	if (res.result === true) {
		success('修改密码成功');
		fire('zjc-000002', 'a002');// 退出登录
	} else {
		error(res.msg);
	}
}
